﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace SDT.CrawlSystem.Servers.Filters
{
    public class RemoveExtraLink
    {
        public static string ExtractAllAHrefTags(string strHtml, string xPath, string specialChars)
        {
            HtmlDocument htmlSnippet = new HtmlDocument();
			var currentHtml = strHtml;
            if (specialChars.Equals("&gt;&gt;"))
            {
                string specialChars1 = ">>";
				currentHtml = strHtml.Replace(specialChars, "").Replace(specialChars1, "");
            }
            if (specialChars.Equals("&gt;"))
            {
				string specialChars2 = "<BR>>";
				currentHtml= strHtml.Replace(specialChars, "").Replace(specialChars2, "");
            }
			htmlSnippet.LoadHtml(currentHtml);
            var resultRemoveLink = htmlSnippet.DocumentNode.SelectNodes(xPath);
            if (resultRemoveLink != null)
            {
                foreach (HtmlNode link in htmlSnippet.DocumentNode.SelectNodes(xPath))
                {
                    link.RemoveAll();
                }
               return htmlSnippet.DocumentNode.WriteContentTo();
            }
			return htmlSnippet.DocumentNode.WriteContentTo();


            //HtmlDocument htmlSnippet = new HtmlDocument();
            //htmlSnippet.LoadHtml(strHtml.Replace(specialChars,""));
            //var resultRemoveLink = htmlSnippet.DocumentNode.SelectNodes(xPath);
            //if (resultRemoveLink != null)
            //{
            //    foreach (HtmlNode link in htmlSnippet.DocumentNode.SelectNodes(xPath))
            //    {
            //        link.RemoveAll();
            //    }
            //    return htmlSnippet.DocumentNode.WriteContentTo();
            //}
            //return strHtml;
        }
    }
}
